package ywh.model
{
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.net.URLRequest;
	import ywh.utils.DataLoader;
	
	/**
	 * ...
	 * @author 杨伟华
	 */
	public class Model1 extends Sprite
	{
		private var chapterPath:String;
		public var btn0:SimpleButton;
		public var btn1:SimpleButton;
		private var btn1_info:String;
		public var btn2:SimpleButton;
		private var btn2_info:String;
		public var btn3:SimpleButton;
		private var btn3_info:String;
		public var btn4:SimpleButton;
		private var btn4_info:String;
		public var panel:MovieClip;
		public var panelContent:TextPanel;
		public var bg:MovieClip;
		public static var _$:Model1;
		public var title:Loader;
		private var _infoProvider:Object;
		private var tname:String;
		
		public function Model1()
		{
			if (_$)
			{
				throw(new Error("Model1类不可有多个实例"));
			}
			else
			{
				_$ = this;
			}
			this.addEventListener(Event.ADDED_TO_STAGE, this_addedToStage);
			title = new Loader();
			title.x = 140;
			title.y = 70;
			addChild(title);
		}
		
		public static function get $():Model1
		{
			return _$ || new Model1();
		}
		
		public function changeChapterHandle():void
		{
			if (!_infoProvider)
			{
				throw(new Error("信息提供者为空"));
			}
			DataLoader.addData(_infoProvider["getResolvePath"]("datas/info.xml"), createData);
			title.load(new URLRequest(_infoProvider["getResolvePath"]("datas/title1.png")));
		}
		
		public function get questionInfo():String
		{
			return btn4_info;
		}
		
		public function set infoProvider(value:Object):void
		{
			_infoProvider = value;
		}		
		
		private function this_addedToStage(e:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE, this_addedToStage);
			initPanel()
			initBtns();
		}
		
		/**
		 * 初始化
		 */
		private function initPanel():void
		{
			this.removeChild(panel);
			panelContent = new TextPanel();
			panelContent.x = 30;
			panelContent.y = 60;
			panelContent.setSize(565, 270, 200);
			panel.addChild(panelContent);
		}
		
		private function createData(data:String):void
		{
			var xmls:XMLList = (new XML(data.replace(/\r\n/g, ""))).*;
			for each (var xml:XML in xmls)
			{
				var id:String = xml.@id[0];
				switch (id)
				{
					case "discribe": 
						btn1_info = xml[0];
						break;
					case "target": 
						btn2_info = xml[0];
						break;
					case "teacher": 
						btn3_info = xml[0];
						break;
					case "question": 
						btn4_info = xml[0];
						break;
					case "tname": 
						tname = xml[0];
						break;
				}
			}
		}
		
		private function initBtns():void
		{
			btn1.addEventListener(MouseEvent.MOUSE_DOWN, showPanel);
			btn2.addEventListener(MouseEvent.MOUSE_DOWN, showPanel);
			btn3.addEventListener(MouseEvent.MOUSE_DOWN, showPanel);
			btn4.addEventListener(MouseEvent.MOUSE_DOWN, btnHandle);
			btn0.addEventListener(MouseEvent.MOUSE_DOWN, btnHandle);
		}
		
		private function btnHandle(e:MouseEvent):void
		{
			switch (e.target)
			{
				case btn0: 
					_infoProvider["map_handle"]();
					break;
				case btn4: 
					_infoProvider["course_handle"]();
					break;
				default: 
					break;
			}
		}
		
		private function showPanel(e:Event):void
		{
			var dp:DisplayObject = e.target as DisplayObject;
			panel.x = dp.x + 15;
			panel.y = dp.y - 20;
			if (panel.parent)
			{
				this.removeChild(panel);
			}
			if (dp.name == "btn3")
			{
				panelContent.setText(this[dp.name + "_info"], _infoProvider["getResolvePath"]("datas/photo.png"), tname);
			}
			else
			{
				panelContent.setText(this[dp.name + "_info"], null);
			}
			this.addChildAt(panel, getChildIndex(dp));
			this.addEventListener(MouseEvent.MOUSE_DOWN, hidePanel);
		}
		
		private function hidePanel(e:MouseEvent):void
		{
			if (e.target == bg || e.target == panel.closeBtn)
			{
				this.removeEventListener(MouseEvent.MOUSE_DOWN, hidePanel);
				if (panel.parent)
				{
					this.removeChild(panel);
				}
			}
		}
	}
}